capture log close
log using $log/AED_dataprep_11_outcomes_macro, text replace

clear
set matsize 800
set more off
/*set max_memory 8g*/
set trace off
   
/* ------------------------------------------------------------------ */
/* --- Aggregate data on labour market characteristics are merged --- */
/* ------------------------------------------------------------------ */
	
		
		
/*----------UE-rate, treatment intensity, vacancy rate-----------*/
		use $orig/BA_Statistik_Summary_short, clear
		tostring monat, replace
		gen jahr = substr(monat,1,4)
		destring monat, replace
		destring jahr, replace
		
		drop if jahr<2001
		drop if jahr>2010
		
		joinby ba monat using $orig/mobihilfen_all, unmatched(master)
		tab _merge
		drop _merge
		
		
		/*Generate treatment intensity for mibility assistance and other ALMP programs*/
		preserve
		
		bys ba jahr: egen best_temp=sum(best_ges) 
		gen best_avg=best_temp/12
		
		foreach  var in uebergang ausruestung fahrkosten trennung umzug reisekosten mobihilfe {
			bys ba jahr: egen zug_`var'_total = max(`var')
		}
		
		foreach type in quali vermleist foerdselbst foerdausb foerdabh foerdabm {
		bys ba jahr: egen zug_`type'_total=sum(almp_`type'_zug)
		}
		
		bys ba jahr: keep if _n==1
		
		
		
		foreach type in quali vermleist foerdselbst foerdausb foerdabh foerdabm uebergang ausruestung fahrkosten trennung umzug reisekosten mobihilfe  {
		gen intensity_`type'=zug_`type'_total/best_avg
		}
		
		keep ba jahr intensity_*
		rename ba iza_wo_aa_
		
		tempfile intensity
		save `intensity', replace
		
		restore
		
		gen vac_rate=gemstellen/best_ges
		gen aloq=best_ges/ziv_erw
	
		rename ba iza_wo_aa_
		rename monat lfd
		
		keep iza_wo_aa_ vac_rate aloq lfd 
		
		
		
		tempfile aloq
		save `aloq', replace
		
	
	
		/*---------GDP + emmigration/immigration rates------------*/
		*Hier aktualisierte BIP Daten integrieren!!!!
		use $orig/kkz_inkar.dta, clear
		rename kkz iza_wo_kreis_
		destring iza_wo_kreis_, replace
		
		gen wanderungen = wandsaldo
		drop wandsaldo
		
		rename year iza_eintritty
		
		tempfile bip
		save `bip', replace
	
		/*Sector Structure*/
		use $orig/sektor_struktur.dta, clear
		rename kkz iza_wo_kreis_
		
		reshape long share_agriculture share_industry share_service, i(iza_wo_kreis_) j (iza_eintritty)
		
		tempfile sector
		save `sector', replace
		
		
foreach iebversion in v901 {
	
	forvalues year=2001/2003 {

		use $data/entries_outcome_`year'_`iebversion',replace
	
	
		
		destring iza_wo_aa_ iza_wo_kreis_,replace
		
		
		gen jahr=.
		forvalues t=0/3 {
		replace jahr=iza_eintritty-`t'
		
		joinby iza_wo_aa_ jahr using `intensity',unmatched(master)
		tab _merge
		drop _merge
		
		foreach type in quali vermleist foerdselbst foerdausb foerdabh foerdabm uebergang ausruestung fahrkosten trennung umzug reisekosten mobihilfe  {
		rename intensity_`type' intensity_`type'_lag`t'
		}/*type*/
		}/*t*/
		drop jahr
	
		joinby iza_wo_aa_ lfd using `aloq',unmatched(master)
		tab _merge
		drop _merge
		
		joinby iza_wo_kreis_ iza_eintritty using `bip',unmatched(master)
		tab _merge
		drop _merge
		
		joinby iza_wo_kreis_ iza_eintritty using $orig/bip_updated.dta ,unmatched(master)
		tab _merge
		drop _merge
		
		/*sektor*/
		joinby iza_wo_kreis_ iza_eintritty using `sector',unmatched(master)
		tab _merge
		drop _merge
		
		
		rename iza_wo_aa_ ba
		
		
	saveold $data/entries_outcome_`year'_`iebversion', replace
	
	
	}
	}
	
	
log close
	
